/**
* 版权所有：版权所有(C) 2014
* 文件编号：
* 文件名称: XMService.java
* 系统编号：
* 系统名称：贝优特项目管理
* 模块编号： 
* 模块名称：项目管理模块
* 设计文件：
* 完成日期：2014-12-01 
* 作    者：lanjinsheng
* 内容摘要：
* 属性描述：
* 方法描述：该类有n个方法：
* 文件调用：单例加载。在Control类中调用
*/
package com.beaut.project.service;

import com.beaut.project.dao.GDMapper;
import com.beaut.project.dao.WareHouseMapper;
import com.beaut.project.dao.XMAssistMapper;
import com.beaut.project.dao.XMMapper;
import com.beaut.project.util.StaticData;
import com.beaut.project.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Transactional(rollbackFor = Exception.class)
@Service("xmService")
public class XMService {
	@Autowired
	private XMMapper xmMapper;
	@Autowired
	WareHouseMapper wareHouseMapper;
	@Autowired
	private GDMapper gdMapper;
	@Autowired
	XMAssistMapper xMAssistMapper;//项目辅助管理
	
	public String checkSameXMCode(Map<String,Object> map){
		return String.valueOf(xmMapper.checkSameXMCode(map));
	}
	
	public String xmFinishBalanceStatus(Map<String,Object> map){
		 xmMapper.xmFinishBalanceStatus(map);
		return Utility.rtJson(Utility.RETURN_OK, "结算完成", null);
	}
	public String addXM(Map<String,Object> map){
    	String xm_id=UUID.randomUUID().toString();
    	map.put("xm_id", xm_id);   	
    	map.put("xm_createtime", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	xmMapper.addXM(map); 
    	List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
    	List<String> gdIds = Arrays.asList(String.valueOf(map.get("subSelValues")).split(","));
    	List<String> gdNames = Arrays.asList(String.valueOf(map.get("subSelTexts")).split(","));
     	List<Map<String,Object>> gdSetList = getGdsetList(map, gdIds,gdNames);
    	List<Map<String,Object>> attList = getAttList(map, atts);
    	if(attList.size() > 0){
    		xmMapper.addXMAttachment(attList);
    	}  
    	if(gdSetList.size() > 0){
    		xmMapper.addXMInitGDSet(gdSetList);
    	} 
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String updateXM(Map<String,Object> map){  	
    	map.put("xm_createtime", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	xmMapper.updateXM(map); 
    	//删除相关从表
		map.put("business_id", map.get("xm_id"));
		map.put("business_type", StaticData.XM_BASE);
    	xmMapper.deleteXMAttachment(map);
    	xmMapper.deleteXMInitGDSet(map);
    	List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
    	List<String> gdIds = Arrays.asList(String.valueOf(map.get("subSelValues")).split(","));
    	List<String> gdNames = Arrays.asList(String.valueOf(map.get("subSelTexts")).split(","));
     	List<Map<String,Object>> gdSetList = getGdsetList(map, gdIds,gdNames);
    	List<Map<String,Object>> attList = getAttList(map, atts);
    	if(attList.size() > 0){
    		xmMapper.addXMAttachment(attList);
    	}  
    	if(gdSetList.size() > 0){
    		xmMapper.addXMInitGDSet(gdSetList);
    	} 
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	/**
	* 撤销未完工的项目
	* @author xf
	* @Time 2017-02-18
	*
	*/
	public String cancelXm(Map<String,Object> map){
		map.put("xm_id", map.get("id"));
		gdMapper.updateGdByXmCancel(map);
		xmMapper.updateTempXm(map);
		xmMapper.updateXmCancel(map);
		return Utility.rtJson(Utility.RETURN_OK, "撤销成功", null);
	}
	public String closeXm(Map<String,Object> map){
		map.put("xm_id", map.get("id"));
		String operation_time = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		Map<String,Object> xm = xmMapper.getXMById(map);
		//将项目库产品转移到供余料库
		  //1.甲供库入工余库
			List<Map<String,Object>> gyDBList = new ArrayList<Map<String,Object>>();//供余库日志
			List<Map<String,Object>> jgDBList = new ArrayList<Map<String,Object>>();//甲供库日志
		   List<Map<String,Object>> jgList =xMAssistMapper.listWarehouseByXmId(map);
		  if(jgList != null && jgList.size() > 0){
				for(int i = 0;i< jgList.size();i++){
					Map<String,Object> gyDBMap = new HashMap<String,Object>();//工余库日志
					Map<String,Object> jgDBMap = new HashMap<String,Object>();//甲供库日志
					gyDBMap.put("id", UUID.randomUUID().toString());
					jgDBMap.put("id", UUID.randomUUID().toString());
					
					Map<String,Object> m = jgList.get(i);
					Map<String,Object> queryParam = new HashMap<String,Object>();
					queryParam.put("dept_id", xm.get("admin_dept_id"));
					queryParam.put("type", 2);//2：工余库
					queryParam.put("product_id", m.get("product_id"));
					Map<String,Object> mySelfMaterialMap= wareHouseMapper.getMySelfMaterial(queryParam);
					
					if(mySelfMaterialMap == null){
						//表示当前该部门没有该材料记录,则添加该记录
						List<Map<String,Object>> dbl = new ArrayList<Map<String,Object>>();
						Map<String,Object> dbMap = new HashMap<String,Object>();
						String warehouse_id = wareHouseMapper.getMySelfWarehouseByDeptId(String.valueOf(xm.get("admin_dept_id")));
						dbMap.put("warehouse_id", warehouse_id);
						dbMap.put("product_id", m.get("product_id"));
						dbMap.put("cost", m.get("cost"));
						dbMap.put("num", m.get("num"));
						dbMap.put("top", 1000);
						dbMap.put("low", 0);
						dbl.add(dbMap);
						wareHouseMapper.addMyMaterialDB(dbl);//工余库增加
						m.put("db_id", m.get("id"));
						wareHouseMapper.subtractDBNum(m);//甲供库减少
						//产生日志
						gyDBMap.put("warehouse_id", warehouse_id);
						jgDBMap.put("warehouse_id", m.get("warehouse_id"));
					}else{
						//获取该产品所有的库存数量和单价
						Map<String,Object> productDBMap = wareHouseMapper.getSelfProductDB(queryParam);
						if(productDBMap == null){
							queryParam.put("cost", m.get("cost"));
						}else{
							//加权平均
							
							BigDecimal oldCost = new BigDecimal(String.valueOf(productDBMap.get("cost")));
							queryParam.put("cost", oldCost);
							wareHouseMapper.updateSelfCost(queryParam);
						}
						//库存增加，生成库存变更日志
						m.put("db_id", mySelfMaterialMap.get("db_id"));
						wareHouseMapper.addDBNum(m);//工余库增加
						gyDBMap.put("warehouse_id", mySelfMaterialMap.get("warehouse_id"));
						m.put("db_id", m.get("id"));
						wareHouseMapper.subtractDBNum(m);//甲供库减少
						jgDBMap.put("warehouse_id", m.get("warehouse_id"));
					}
					gyDBMap.put("customer_id", "");
					gyDBMap.put("project_id", "");
					gyDBMap.put("gd_id", "");
					gyDBMap.put("supplier_id", "");
					gyDBMap.put("product_id", m.get("product_id"));
					gyDBMap.put("num", m.get("num"));
					gyDBMap.put("cost", m.get("cost"));
					gyDBMap.put("person_id", map.get("person_id"));
					gyDBMap.put("person", map.get("person"));
					gyDBMap.put("operation_date", operation_time);
					gyDBMap.put("direction", StaticData.DB_IN);
					gyDBMap.put("business_type", StaticData.XM_JS_IN);
					gyDBMap.put("business_id", "");//无业务展示
					gyDBMap.put("summary", "项目【"+String.valueOf(xm.get("xm_code"))+"】结算后甲供库入工余库，库存增加");
					gyDBList.add(gyDBMap);
					
					jgDBMap.put("customer_id", "");
					jgDBMap.put("project_id", "");
					jgDBMap.put("gd_id", m.get("gd_id"));
					jgDBMap.put("supplier_id", "");
					jgDBMap.put("product_id", m.get("product_id"));
					jgDBMap.put("num", m.get("num"));
					jgDBMap.put("project_id", xm.get("id"));
					jgDBMap.put("cost", m.get("cost"));
					jgDBMap.put("person_id", map.get("person_id"));
					jgDBMap.put("person", map.get("person"));
					jgDBMap.put("operation_date", operation_time);
					jgDBMap.put("direction", StaticData.DB_OUT);
					jgDBMap.put("business_type", StaticData.XM_JS_OUT);
					jgDBMap.put("business_id", "");//无业务展示
					jgDBMap.put("summary", "项目【"+String.valueOf(xm.get("xm_code"))+"】结算后甲供库入工余库，库存减少");
					jgDBList.add(jgDBMap);
				}
				wareHouseMapper.addDBLog1(gyDBList);
				wareHouseMapper.addDBLog1(jgDBList);
			}
		xmMapper.closeXm(map); 
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	
	public String addXMLy(Map<String,Object> map){
		String ly_id=UUID.randomUUID().toString();
		map.put("ly_id", ly_id);   	
		map.put("flow_status",StaticData.NEW_CREATE);
		map.put("business_id",ly_id);
		map.put("business_type",StaticData.XM_LY);
		xmMapper.addXMLy(map); 
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		} 
		
		//项目辅助状态处理
		Map<String,Object> mstatus=new HashMap<String, Object>();
		mstatus.put("xm_id", map.get("xm_id"));
		mstatus.put("xm_business_step", StaticData.XM_HAS_LY);
		xMAssistMapper.addXMBusinessStep(mstatus);
		
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addXmCheck(Map<String,Object> map){
		String id=UUID.randomUUID().toString();
		map.put("id", id);   	
		map.put("status",StaticData.NEW_CREATE);
		map.put("business_id",id);
		map.put("business_type",StaticData.XM_CHECK);
		xmMapper.addXmCheck(map); 
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		}  
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addContract(Map<String,Object> map){
		String id=UUID.randomUUID().toString();
		map.put("id", id);   	
		map.put("status",StaticData.NEW_CREATE);
		map.put("business_id",id);
		map.put("business_type",StaticData.XM_CONTRACT);
		xmMapper.addContract(map); 
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		}  
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateContract(Map<String,Object> map){  	
		//删除相关从表
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.XM_CONTRACT);
		xmMapper.updateContract(map);
		xmMapper.deleteXMAttachment(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		}  
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String addXmEvaluation(Map<String,Object> map){
		//添加的时候，如果此项目已经发生过经济考评，那么只需要添加考评明细，主体内容总利润和分摊利润累加。
		String fin_eval_id="";
			fin_eval_id = UUID.randomUUID().toString();
		map.put("id", fin_eval_id);   	
		map.put("status",StaticData.NEW_CREATE);
		xmMapper.addXmEvaluation(map); 
		List<Map<String,String>> evalList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("evlaArr"));
		//查询部门分摊利润明细当前部门是否存在，如果存在，那么jingx
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("fin_eval_id", fin_eval_id);
				evalList.add(m);
			}
		}
		if(evalList.size() > 0){
			xmMapper.addXmEvaluationDetail(evalList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
		
	}
	public String updateXmEvaluation(Map<String,Object> map){  	
		//删除相关从表
		map.put("fin_eval_id", map.get("id"));
		xmMapper.updateXmEvaluation(map);
		xmMapper.deleteXmEvaluationDetail(map);
		List<Map<String,String>> evalList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("evlaArr"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("fin_eval_id", String.valueOf(map.get("id")));
				evalList.add(m);
			}
		}
		if(evalList.size() > 0){
			xmMapper.addXmEvaluationDetail(evalList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteXmEvaluation(Map<String,Object> map){
			xmMapper.deleteXmEvaluation(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateXMLy(Map<String,Object> map){  	
		//删除相关从表
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.XM_LY);
		xmMapper.updateXMLy(map);
		xmMapper.deleteXMAttachment(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		}  
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}

	public String updateXmCheck(Map<String,Object> map){  	
		//删除相关从表
		map.put("business_id", map.get("id"));
		map.put("business_type", StaticData.XM_CHECK);
		xmMapper.updateXmCheck(map);
		xmMapper.deleteXMAttachment(map);
		List<String> atts = Arrays.asList(String.valueOf(map.get("attachmentId")).split(","));
		List<Map<String,Object>> attList = getAttList2(map, atts);
		if(attList.size() > 0){
			xmMapper.addXMAttachment(attList);
		}  
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteXMLy(Map<String,Object> map){
		Map<String,Object>mapLy=null;
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			mapLy=new HashMap<String,Object>();
			mapLy.put("id", list.get(i));
			xmMapper.deleteXMLy(mapLy);
			map.put("business_id", list.get(i));
			map.put("business_type", StaticData.XM_LY);
	    	xmMapper.deleteXMAttachment(map);
			
		}
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteXMCheck(Map<String,Object> map){
		Map<String,Object>mapLy=null;
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			mapLy=new HashMap<String,Object>();
			mapLy.put("id", list.get(i));
			xmMapper.deleteXMCheck(mapLy);
			map.put("business_id", list.get(i));
			map.put("business_type", StaticData.XM_CHECK);
	    	xmMapper.deleteXMAttachment(map);
			
		}
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteContract(Map<String,Object> map){
		Map<String,Object>mapContract=null;
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		for(int i=0;i<list.size();i++){
			mapContract=new HashMap<String,Object>();
			mapContract.put("id", list.get(i));
			xmMapper.deleteXMCheck(mapContract);
			map.put("business_id", list.get(i));
			map.put("business_type", StaticData.XM_CONTRACT);
	    	xmMapper.deleteXMAttachment(map);
		}
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String deleteXm(Map<String,Object> map){
			xmMapper.deleteXM(map);
			map.put("business_id", map.get("id"));
			map.put("business_type", StaticData.XM_BASE);
	    	xmMapper.deleteXMAttachment(map);
	    	map.put("xm_id", map.get("id"));
	    	xmMapper.deleteXMInitGDSet(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}	
	
	
	
	public String deleteJGMaterial(Map<String,Object> map){
		xmMapper.deleteFlowInWarehouseDetail(map);
		xmMapper.deleteFlowInWarehouse(map);
	   return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteJGOutMaterial(Map<String,Object> map){
		xmMapper.deleteFlowOutWarehouseDetail(map);
		xmMapper.deleteFlowOutWarehouse(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateGdSets(Map<String,Object> map){  	

    	xmMapper.deleteXMGDSet(map); 
    	List<String> gdIds = Arrays.asList(String.valueOf(map.get("subSelValues")).split(","));
    	map.put("list", gdIds);
    	if(gdIds.size() > 0){
    		xmMapper.updateXMGDSet(map);
    		//更新预算值
    		BigDecimal sumMoney = new BigDecimal(0);
    		BigDecimal sumOtherMoeny = new BigDecimal(0);
    		BigDecimal sumBudget = new BigDecimal(0);
    		for(int i=0;i<gdIds.size();i++){
    			Map<String,Object> queryMap = new HashMap<String,Object>();
    			queryMap.put("gd_id", gdIds.get(i));
    			Map<String,Object> contentMap = gdMapper.getGdAssMaterial(queryMap);
    			sumMoney = sumMoney.add(new BigDecimal(String.valueOf(contentMap.get("sumMoney"))));
    			sumOtherMoeny = sumOtherMoeny.add(new BigDecimal(String.valueOf(contentMap.get("otherMoney"))));
    			sumBudget = sumBudget.add(new BigDecimal(String.valueOf(contentMap.get("budget"))));
    		}
    		
    		Map<String,Object> queryMap = new HashMap<String,Object>();
    		queryMap.put("xm_id", map.get("xm_id"));
    		queryMap.put("sumMoney", sumMoney.add(sumOtherMoeny).add(sumBudget));
    		queryMap.put("estimate_work_price", sumBudget);//预估施工费
    		queryMap.put("estimate_material_price", sumMoney);//预估材料费
    		queryMap.put("estimate_other_price", sumOtherMoeny);//预估其他费
    		xmMapper.updateXMBudget(queryMap);
    	}  
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String flowBackToXMBaseStatus(Map<String,Object> map){ 
		map.put("xm_id", map.get("key"));
		map.put("xm_createtime", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
    	xmMapper.updateXMCreateTime(map); 
		List<Map<String,Object>> list= xmMapper.listInitGDSetByXMId(map);
		if(list.size()==0){
			return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
		}
		Map<String,Object> xmMap=xmMapper.getXMById(map);
		map.put("list", list);
		map.put("xm_name", xmMap.get("xm_name"));	
		map.put("xm_code", xmMap.get("xm_code"));
    	xmMapper.flowBackToXMBaseStatus(map);
    	
    	map.put("xm_business_step", StaticData.XM_INIT_PASS);
		xMAssistMapper.addXMBusinessStep(map);
		
		//删除缓存中的工单集
		xmMapper.deleteXMInitGDSet(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String getXmIsHas(Map<String,Object> map){
	    int isHas = xmMapper.getXmIsHas(map);
	    if(isHas>0){
	    	return Utility.rtJson("1", "此项目编号或者项目名称已经存在！", null); 
	    }
		return Utility.rtJson("0", "", null); 
	}
	public String getXMById(Map<String,Object> map){
		Map<String,Object> xmMap= xmMapper.getXMById(map);
		map.put("business_id", map.get("xm_id"));
		map.put("business_type", StaticData.XM_BASE);
		
		List<Map<String,String>> attList = xmMapper.getXMTempAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		StringBuilder gdIds = new StringBuilder("");
		StringBuilder gdNames = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		xmMap.put("attachmentId", attachmentId.toString());
		xmMap.put("attachmentName", attachmentName.toString());
		List<Map<String,Object>> gdSetsList =new ArrayList<Map<String,Object>>();
		if(Integer.parseInt(String.valueOf(xmMap.get("xm_flow_status")))==3){
			gdSetsList = gdMapper.listGdByXmId(map);
		}else{
			gdSetsList = xmMapper.listInitGDSetByXMId(map);
		}
		if(gdSetsList != null && gdSetsList.size() > 0){
			for(int i=0;i<gdSetsList.size();i++){
				Map<String,Object> m = gdSetsList.get(i);
				if(m != null){
					if(i==0){
						gdIds.append(m.get("gd_id"));
						gdNames.append(m.get("gd_name"));
					}else{
						gdIds.append(",").append(m.get("gd_id"));
						gdNames.append(",").append(m.get("gd_name"));
					}
				}
			}
		}
		xmMap.put("gdIds", gdIds.toString());
		xmMap.put("gdNames", gdNames.toString());
		return Utility.toJson(xmMap);
	}
	public String getAlreadyOutInvoice(Map<String,Object> map){
		Map<String,Object> xmMap= xmMapper.getXMById(map);
		return String.valueOf(xmMap.get("out_invoice"));
	}
	public String getEndInMoneyXMById(Map<String,Object> map){
		Map<String,Object> xmMap= xmMapper.getEndInMoneyXMById(map);
		return Utility.toJson(xmMap);
	}
	public String getBalancePayStatus(Map<String,Object> map){
		int  status = xmMapper.getBalancePayStatus(map);
		return Utility.rtJson(String.valueOf(status), "", null);
	}
	
	public String getXmHasFinance(Map<String,Object> map){

		//查询该工单是否有材料发生变化
		Map<String,Object>mapLl=xmMapper.getMaterialCount(map);
		if(mapLl != null){
			if(Integer.parseInt(String.valueOf(mapLl.get("countNum")))>0){
				return Utility.rtJson(Utility.RETURN_OK, "此工单已经发生材料变化，所以不能移除", null);
			}
		}
		//是否有采购支付，和分包商支付
		Map<String,Object> mapPay = xmMapper.getXMPayCount(map);
		if(mapPay != null){
			if(Integer.parseInt(String.valueOf(mapPay.get("countPay")))>0){
				return Utility.rtJson(Utility.RETURN_OK, "此工单已经发生了采购支付或者分包商支付，所以不能移除", null);
			}
		}
		//查询是否有借款报账
		Map<String,Object> mapLoan = xmMapper.getReimLoanCount(map);
		if(mapLoan != null){
		if(Integer.parseInt(String.valueOf(mapLoan.get("countLoan")))>0){
			return Utility.rtJson(Utility.RETURN_OK, "此工单已经发生借款报账，所以不能移除", null);
		}
		}
		return Utility.rtJson(Utility.RETURN_ERROR, "", null);
	}
	
	public String getXMGDSets(Map<String,Object> map){
		Map<String,Object> xmMap= new HashMap();
		StringBuilder gdIds = new StringBuilder("");
		StringBuilder gdNames = new StringBuilder("");

		
		List<Map<String,Object>> gdSetsList = xmMapper.listGDSetByXMId(map);
		if(gdSetsList != null && gdSetsList.size() > 0){
			for(int i=0;i<gdSetsList.size();i++){
				Map<String,Object> m = gdSetsList.get(i);
				if(m != null){
					if(i==0){
						gdIds.append(m.get("gd_id"));
						gdNames.append(m.get("gd_name"));
					}else{
						gdIds.append(",").append(m.get("gd_id"));
						gdNames.append(",").append(m.get("gd_name"));
					}
				}
			}
		}
		xmMap.put("gdIds", gdIds.toString());
		xmMap.put("gdNames", gdNames.toString());
		return Utility.toJson(xmMap);
	}
	public String getXmvaluationById(Map<String,Object> map){
		return Utility.toJson(xmMapper.getXmvaluationById(map));
	}
	
	public String listPageXMBase(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXM(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXmCg(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXmCg(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWarehouseXM(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageWarehouseXM(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXMBaseEnd(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXMBaseEnd(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	public String listPageXMAndGdClose(Map<String,Object> map){
		List<Map<String,Object>> listXm = xmMapper.listPageXMAndGdClose(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(listXm));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	

	public String listPageFlowInWarehouse(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageFlowInWarehouse(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFlowOutWarehouse(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageFlowOutWarehouse(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getJGMaterial(Map<String,Object> map){
		Map<String,Object> jgMap= xmMapper.getFlowInWarehouse(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.toJson(jgMap));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getJGOutMaterial(Map<String,Object> map){
		Map<String,Object> jgMap= xmMapper.getFlowOutWarehouse(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.toJson(jgMap));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getXmEvalById(Map<String,Object> map){
		Map<String,Object> profitMap= xmMapper.getXmEvalById(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.toJson(profitMap));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getXmEvalByXmId(Map<String,Object> map){
		Map<String,Object> evalMap= xmMapper.getXmEvalByXmId(map);
		
		return Utility.toJson(evalMap);
	}
	public String getXmCheckById(Map<String,Object> map){
		Map<String,Object> checkMap= xmMapper.getXmCheckById(map);
		map.put("business_type", StaticData.XM_CHECK);
		map.put("business_id", map.get("id"));
		List<Map<String,String>> attList = xmMapper.getXMTempAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		checkMap.put("attachmentId", attachmentId.toString());
		checkMap.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(checkMap);
	}
	public String getXmContractById(Map<String,Object> map){
		Map<String,Object> contractMap= xmMapper.getXmContractById(map);
		map.put("business_type", StaticData.XM_CONTRACT);
		map.put("business_id", map.get("id"));
		List<Map<String,String>> attList = xmMapper.getXMTempAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		contractMap.put("attachmentId", attachmentId.toString());
		contractMap.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(contractMap);
	}

	public String listPageLy(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageLy(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageCheck(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageCheck(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("check_status", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listXmDept(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listXmDept(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listXmvaluationDetail(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listXmvaluationDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getXMLyById(Map<String,Object> map){
		Map<String,Object> baseMap = xmMapper.getXMLyById(map);
		//工单附件
		map.put("business_type", StaticData.XM_LY);
		map.put("business_id", map.get("id"));
		List<Map<String,String>> attList = xmMapper.getXMTempAttachmentByGDId(map);
		StringBuilder attachmentId = new StringBuilder("");
		StringBuilder attachmentName = new StringBuilder("");
		if(attList != null && attList.size() > 0){
			for(int i=0;i<attList.size();i++){
				Map<String,String> m = attList.get(i);
				if(m != null){
					if(i==0){
						attachmentId.append(m.get("attachmentId"));
						attachmentName.append(m.get("attachmentName"));
					}else{
						attachmentId.append(",").append(m.get("attachmentId"));
						attachmentName.append("*").append(m.get("attachmentName"));
					}
				}
			}
		}
		baseMap.put("attachmentId", attachmentId.toString());
		baseMap.put("attachmentName", attachmentName.toString());
		
		return Utility.toJson(baseMap);
	}
	public String listAllPageGDSet(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listAllPageGDSet(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
			map.put("gd_id", list.get(i).get("id"));
			map.put("type", 0);
			int balance = gdMapper.hasBalance(map);//甲供
			map.put("type",1);
			int balance1 = gdMapper.hasBalance(map);//乙供
			if(balance == 0 && balance1 == 0){
				list.get(i).put("no_material","1");
			}else{
				list.get(i).put("no_material","0");
			}
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageEvaluation(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageEvaluation(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXmReimDetail(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXmReimDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageBorrowMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageBorrowMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXmContract(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXmContract(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_name", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listInitGDSetByXMId(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listInitGDSetByXMId(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listXMProcessStatuc(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.NOSTART));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.NOSTART)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.ONLINECREATE));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.ONLINECREATE)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.FINISH));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.FINISH)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.CHECK));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.CHECK)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.SUBMISSION));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.SUBMISSION)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.INVOICE));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.INVOICE)));
		list.add(map);
		
		map = new HashMap<String,Object>();
		map.put("id", String.valueOf(StaticData.SETTLE));
		map.put("text", StaticData.XM_PROCESS_STATUS.get(String.valueOf(StaticData.SETTLE)));
		list.add(map);
		return Utility.lstMaptoJson(list);
	}
	public String listGdByXmId(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listGdByXmId(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listGDSetByXMId(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listGDSetByXMId(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	private List<Map<String,Object>> getGdsetList(Map<String,Object> map,List<String> gdIds,List<String> gdNames){
		List<Map<String,Object>> gdSetList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<gdIds.size();i++){
			if(!Utility.isNullorEmpty(gdIds.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("xm_id", map.get("xm_id"));
				m.put("gd_name",gdNames.get(i));
				m.put("gd_id", gdIds.get(i));
				gdSetList.add(m);
			}
		}
		return gdSetList;
	}
	
	private List<Map<String,Object>> getAttList(Map<String,Object> map,List<String> list){
		List<Map<String,Object>> attList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			if(!Utility.isNullorEmpty(list.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
				m.put("user_id", map.get("att_user_id"));
				m.put("user_name", map.get("att_user_name"));
				m.put("xm_id", map.get("xm_id"));
				m.put("business_id", map.get("xm_id"));
				m.put("attachment_id", list.get(i));
				m.put("business_type", StaticData.XM_BASE);
				attList.add(m);
			}
		}
		return attList;
	}

	public String addBeforeJGMaterialDB(Map<String,Object> map){
//		String warehouse_id = String.valueOf(map.get("warehouse_id"));
//		String customer_id = String.valueOf(map.get("customer_id"));
    	String products = String.valueOf(map.get("products"));
    	String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
    	map.put("operation_date", operation_date);
    	map.put("id", UUID.randomUUID().toString());
    	map.put("status", 0);
    	String[] productArr=products.split(";");
    	
    	List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
    	for(int i=0;i<productArr.length;i++){
    		Map<String,String> productMap= Utility.json2Map(productArr[i]);
    		Map<String,Object> m= new HashMap<String,Object>();
    		m.put("request_id", map.get("id"));
    		m.put("material_id", productMap.get("material_id"));
    		m.put("num", productMap.get("num"));
    		dbList.add(m);
    	}
    	xmMapper.addXMFlowInWarehouse(map);
    	xmMapper.addXMFlowInDetail(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addBeforeJGOutMaterialDB(Map<String,Object> map){
//		String warehouse_id = String.valueOf(map.get("warehouse_id"));
//		String customer_id = String.valueOf(map.get("customer_id"));
		String products = String.valueOf(map.get("products"));
		String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		map.put("operation_date", operation_date);
		map.put("id", UUID.randomUUID().toString());
		map.put("status", 0);
		String[] productArr=products.split(";");
		
		List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<productArr.length;i++){
			Map<String,String> productMap= Utility.json2Map(productArr[i]);
			Map<String,Object> m= new HashMap<String,Object>();
			m.put("request_id", map.get("id"));
			m.put("material_id", productMap.get("material_id"));
			m.put("num", productMap.get("num"));
			m.put("db_num", productMap.get("db_num"));
			dbList.add(m);
		}
		xmMapper.addXMFlowOutWarehouse(map);
		xmMapper.addXMFlowOutDetail(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateBeforeJGMaterialDB(Map<String,Object> map){
//		String warehouse_id = String.valueOf(map.get("warehouse_id"));
//		String customer_id = String.valueOf(map.get("customer_id"));
    	String products = String.valueOf(map.get("products"));
    	String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
    	map.put("operation_date", operation_date);
//    	map.put("id", UUID.randomUUID().toString());
    	map.put("status", 0);
    	String[] productArr=products.split(";");
    	List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
    	for(int i=0;i<productArr.length;i++){
    		Map<String,String> productMap= Utility.json2Map(productArr[i]);
    		Map<String,Object> m= new HashMap<String,Object>();
    		m.put("request_id", map.get("id"));
    		m.put("material_id", productMap.get("material_id"));
    		m.put("num", productMap.get("num"));
    		dbList.add(m);
    	}
    	xmMapper.updateXMFlowInWarehouse(map);
    	xmMapper.deleteFlowInWarehouseDetail(map);
    	xmMapper.addXMFlowInDetail(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateBeforeJGOutMaterialDB(Map<String,Object> map){
//		String warehouse_id = String.valueOf(map.get("warehouse_id"));
//		String customer_id = String.valueOf(map.get("customer_id"));
		String products = String.valueOf(map.get("products"));
		String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		map.put("operation_date", operation_date);
//    	map.put("id", UUID.randomUUID().toString());
		map.put("status", 0);
		String[] productArr=products.split(";");
		List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<productArr.length;i++){
			Map<String,String> productMap= Utility.json2Map(productArr[i]);
			Map<String,Object> m= new HashMap<String,Object>();
			m.put("request_id", map.get("id"));
			m.put("material_id", productMap.get("material_id"));
			m.put("num", productMap.get("num"));
			m.put("db_num", productMap.get("db_num"));
			dbList.add(m);
		}
		xmMapper.updateXMFlowOutWarehouse(map);
		xmMapper.deleteFlowOutWarehouseDetail(map);
		xmMapper.addXMFlowOutDetail(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String getXMFlowInDetail(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.getXMFlowInDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
   }
	public String getXMFlowOutDetail(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.getXMFlowOutDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	
	public String flowBackToXMFlowInWarehouseStatus(Map<String,Object> map){ 
		map.put("request_id", map.get("key"));
		map.put("id", map.get("key"));
		Map<String,Object> mapJG = xmMapper.getFlowInWarehouse(map);
		List<Map<String,Object>> list=xmMapper.getXMFlowBackInWarehouse(map);
		if(mapJG!=null){
//		List<Map<String,Object>> listDBLog=new ArrayList();
		for(int i=0;i<list.size();i++){
			if("1".equals(String.valueOf(mapJG.get("flag")))){
				//甲供材正常入库
				if(list.get(i)!=null){
					Map<String,Object> m= list.get(i);
					Map<String,Object>mapR=new HashMap<String,Object>();
					mapR.put("xm_id", m.get("xm_id"));
					mapR.put("material_id", m.get("material_id"));
					mapR.put("warehouse_id", m.get("warehouse_id"));
					Map<String,Object> mr=xmMapper.getMaterialDb(mapR);
					if(mr!=null && mr.get("id")!=null){
						//更新数值
						BigDecimal d = new BigDecimal(String.valueOf(mr.get("num"))); 
						BigDecimal d2 = new BigDecimal(String.valueOf(m.get("num"))); 
						mr.put("num2", d.add(d2));
						xmMapper.flowBackToMaterialDb(mr);
						m.put("business_id",m.get("wId"));
					}
					else{
						//插入数值
						String id=UUID.randomUUID().toString();
						m.put("mId", id);
						xmMapper.addXMFlowBackMaterialDb(m);
						m.put("business_id",m.get("wId"));
					}
					m.put("direction", StaticData.DB_IN);
					m.put("business_type", StaticData.XM_JG_IN_DB);
					StringBuffer summary=new StringBuffer();
	//				 "工单【"+String.valueOf(jgRequestMap.get("gd_code"))+"】借【"+myJgMaterialMap.get("warehouse_name")+"】"
					summary=new StringBuffer("项目【").append(m.get("xm_code")).append("】甲供入库【").append(m.get("warehouse")).append("】").append(m.get("num"));
					m.put("summary", summary.toString());
					
				}
			}else{
				//客户库借料入库
				if(list.get(i)!=null){
					StringBuffer summary=new StringBuffer();
					Map<String,Object>mapR=new HashMap<String,Object>();
					Map<String,Object> m= list.get(i);
					mapR.put("xm_id", m.get("xm_id"));
					mapR.put("material_id", m.get("material_id"));
					mapR.put("warehouse_id", m.get("warehouse_id"));
					Map<String,Object> mr=xmMapper.getMaterialDb(mapR);
					if(mr!=null && mr.get("id")!=null){
						//更新数值
						BigDecimal d = new BigDecimal(String.valueOf(mr.get("num"))); 
						BigDecimal d2 = new BigDecimal(String.valueOf(m.get("num"))); 
						mr.put("num2", d.add(d2));
						mr.put("num3", d.subtract(d2));
						xmMapper.flowBackToMaterialDb(mr);
						m.put("business_id",m.get("wId"));
					}
					else{
						//插入数值
						String id=UUID.randomUUID().toString();
						m.put("mId", id);
						xmMapper.addXMFlowBackMaterialDb(m);
						m.put("business_id",m.get("wId"));
					}
					m.put("direction", StaticData.DB_IN);
					m.put("business_type", StaticData.XM_JG_IN_DB);
					
	//				 "工单【"+String.valueOf(jgRequestMap.get("gd_code"))+"】借【"+myJgMaterialMap.get("warehouse_name")+"】"
					summary=new StringBuffer("项目【").append(m.get("xm_code")).append("】借客户料入库【").append(m.get("warehouse")).append("】").append(m.get("num"));
					m.put("summary", summary.toString());
					
				}
			
			}
		}
    	xmMapper.addXMFlowBackDbLog(list);
		}
		if("2".equals(String.valueOf(mapJG.get("flag")))){
			//减少客户库库存
	    	for(int i=0;i<list.size();i++){
				//客户库借料入库
				if(list.get(i)!=null){
					StringBuffer summary=new StringBuffer();
					Map<String,Object> m= list.get(i);
					Map<String,Object> mr=xmMapper.getMaterialDbTwo(m);
					if(mr!=null && mr.get("id")!=null){
						//更新数值
						m.put("warehouse_id", mr.get("warehouse_id"));
						BigDecimal d = new BigDecimal(String.valueOf(mr.get("num"))); 
						BigDecimal d2 = new BigDecimal(String.valueOf(m.get("num"))); 
						mr.put("num2", d.subtract(d2));
						xmMapper.flowBackToMaterialDb(mr);
						m.put("business_id",m.get("wId"));
					}
					m.put("direction", StaticData.DB_OUT);
					m.put("business_type", StaticData.XM_JG_IN_DB);
					
	//				 "工单【"+String.valueOf(jgRequestMap.get("gd_code"))+"】借【"+myJgMaterialMap.get("warehouse_name")+"】"
					summary=new StringBuffer("项目【").append(m.get("xm_code")).append("】入甲供材库出库【").append(m.get("warehouse")).append("】").append(m.get("num"));
					m.put("summary", summary.toString());
					
					      
					
				}
		}
	    	xmMapper.addXMFlowBackDbLog1(list);
		}
    	return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);		
	}
	//甲供材出库审批回调
	public String backFlowToXMFlowOutWarehouseStatus(Map<String,Object> map){ 
		map.put("request_id", map.get("key"));
		map.put("id", map.get("key"));
		Map<String,Object> mapJG = xmMapper.getFlowOutWarehouse(map);
		List<Map<String,Object>> list=xmMapper.getXMFlowBackOutWarehouse(map);
		if(mapJG!=null){
			for(int i=0;i<list.size();i++){
					//甲供材正常出库
					if(list.get(i)!=null){
						Map<String,Object> m= list.get(i);
						Map<String,Object>mapR=new HashMap<String,Object>();
						mapR.put("xm_id", m.get("xm_id"));
						mapR.put("material_id", m.get("material_id"));
						mapR.put("warehouse_id", m.get("warehouse_id"));
						Map<String,Object> mr=xmMapper.getMaterialDb(mapR);
						if(mr!=null && mr.get("id")!=null){
							//更新数值
							BigDecimal d = new BigDecimal(String.valueOf(mr.get("num"))); 
							BigDecimal d2 = new BigDecimal(String.valueOf(m.get("num"))); 
							mr.put("num2", d.subtract(d2));
							xmMapper.flowBackToMaterialDb(mr);
							m.put("business_id",m.get("wId"));
						}
						m.put("direction", StaticData.DB_OUT);
						m.put("business_type", StaticData.XM_JGC_OUT_DB);
						StringBuffer summary=new StringBuffer();
						//				 "工单【"+String.valueOf(jgRequestMap.get("gd_code"))+"】借【"+myJgMaterialMap.get("warehouse_name")+"】"
						summary=new StringBuffer("项目【").append(m.get("xm_code")).append("】甲供出库【").append(m.get("warehouse")).append("】").append(m.get("num"));
						m.put("summary", summary.toString());
						
					}
			}
			xmMapper.addXMFlowBackDbLog(list);
		}
		if("2".equals(String.valueOf(mapJG.get("flag")))){
			//减少客户库库存
			for(int i=0;i<list.size();i++){
				//客户库借料入库
				if(list.get(i)!=null){
					StringBuffer summary=new StringBuffer();
					Map<String,Object> m= list.get(i);
					Map<String,Object> mr=xmMapper.getMaterialDbTwo(m);
					if(mr!=null && mr.get("id")!=null){
						//更新数值
						m.put("warehouse_id", mr.get("warehouse_id"));
						BigDecimal d = new BigDecimal(String.valueOf(mr.get("num"))); 
						BigDecimal d2 = new BigDecimal(String.valueOf(m.get("num"))); 
						mr.put("num2", d.subtract(d2));
						xmMapper.flowBackToMaterialDb(mr);
						m.put("business_id",m.get("wId"));
					}
					m.put("direction", StaticData.DB_OUT);
					m.put("business_type", StaticData.XM_JG_IN_DB);
					
					//				 "工单【"+String.valueOf(jgRequestMap.get("gd_code"))+"】借【"+myJgMaterialMap.get("warehouse_name")+"】"
					summary=new StringBuffer("项目【").append(m.get("xm_code")).append("】入甲供材库出库【").append(m.get("warehouse")).append("】").append(m.get("num"));
					m.put("summary", summary.toString());
					
					
					
				}
			}
			xmMapper.addXMFlowBackDbLog1(list);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);		
	}
	
	
	private List<Map<String,Object>> getAttList2(Map<String,Object> map,List<String> list){
		List<Map<String,Object>> attList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			if(!Utility.isNullorEmpty(list.get(i))){
				Map<String,Object> m= new HashMap<String,Object>();
				m.put("operation_date", new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date()));
				m.put("user_id", map.get("att_user_id"));
				m.put("user_name", map.get("att_user_name"));
				m.put("xm_id", map.get("xm_id"));
				m.put("business_id", map.get("business_id"));
				m.put("attachment_id", list.get(i));
				m.put("business_type", map.get("business_type"));
				attList.add(m);
			}
		}
		return attList;
	}
	
	
	public String listXMGDBalance(Map<String,Object> map){
			return Utility.lstMaptoJson(xmMapper.listXMGDBalance(map));
	}
	public String listXMGDBalanceByXmId(Map<String,Object> map){
		return Utility.lstMaptoJson(xmMapper.listXMGDBalanceByXmId(map));
	}
	public String addXMGDBalance1(Map<String,Object> map){
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("xm_id", String.valueOf(map.get("xm_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			xmMapper.addXMGDBalance1(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addXMGDBalance(Map<String,Object> map){
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("xm_id", String.valueOf(map.get("xm_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			xmMapper.addXMGDBalance(proList);
		}
		Map<String,Object> m2=new HashMap<String, Object>();
		m2.put("xm_id", map.get("xm_id"));
		m2.put("xm_material_balance_status", 0);
		xmMapper.updateMaterialBalanceStatus(m2);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateXMGDBalance1(Map<String,Object> map){
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				xmMapper.updateXMGDBalance1(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateXMGDBalance(Map<String,Object> map){
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				xmMapper.updateXMGDBalance(m);
			}
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String hasXMBalance(Map<String,Object> map){
		return String.valueOf(xmMapper.hasXMBalance(map));
	}
	

	
	public String listPageXMMaterBalance(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXMMaterBalance(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXMInMoneyBalance(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXMInMoneyBalance(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXMPaymentBalance(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageXMPaymentBalance(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFinanceInfo(Map<String,Object> map){
		//收入---已开票
		List<Map<String,Object>> list= xmMapper.listPageFinanceInfo(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWorker(Map<String,Object> map){
		List<Map<String,Object>> list= xmMapper.listPageWorker(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getWorkerBugdet(Map<String,Object> map){
		return String.valueOf(xmMapper.getWorkerBugdet(map));
	}
}
